package ch.netcetera.planningpoker.client.ui.round;

import java.util.List;
import java.util.logging.Logger;

import ch.netcetera.planningpoker.client.model.ParticipantProxy;
import ch.netcetera.planningpoker.client.model.RoundProxy;
import ch.netcetera.planningpoker.client.ui.card.CardView;
import ch.netcetera.planningpoker.client.ui.events.RoundStatusChangedEvent;
import ch.netcetera.planningpoker.client.ui.events.RoundStatusChangedEventHandler;
import ch.netcetera.planningpoker.shared.GameRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.Proxy;

public class RoundPresenter extends
        Presenter<RoundPresenter.IView, RoundPresenter.IProxy> {

    private static final Logger logger = Logger.getLogger(RoundPresenter.class
            .getName());

    @ProxyStandard
    public interface IProxy extends Proxy<RoundPresenter> {

    }

    public interface IView extends View {
        
        HasText getRoundTitle();
        
        HasWidgets getCards();
        
    }

    private GameRequestFactory requestFactory;


    @Inject
    public RoundPresenter(EventBus eventBus, 
            IView view, IProxy proxy, GameRequestFactory requestFactory) {
        super(true, eventBus, view, proxy);
        this.requestFactory = requestFactory;
    }


    @Override
    protected void onBind() {
        getEventBus().addHandler(RoundStatusChangedEvent.TYPE, new RoundStatusChangedEventHandler() {
            
            @Override
            public void onRoundStatusChanged(RoundStatusChangedEvent event) {
                logger.info("Round status changed loading open round for id: " + event.getGameId());
                loadOpenRound(event.getGameId());
            }
        });
    }

    protected void loadOpenRound(Long gameId) {
        requestFactory.getRoundRequest().getOpenRoundForGame(gameId).fire(new Receiver<RoundProxy>() {

            @Override
            public void onSuccess(RoundProxy response) {
                if (response != null) {
                    displayOpenRound(response);
                }
            }
            
        });
        requestFactory.getParticipantRequest().getParticipantsForGame(gameId).fire(new Receiver<List<ParticipantProxy>>() {

            @Override
            public void onSuccess(List<ParticipantProxy> participants) {
                getView().getCards().clear();
                for (ParticipantProxy participant : participants) {
                    CardView card = GWT.create(CardView.class);
                    card.getParticipantName().setText(participant.getName());
                    getView().getCards().add(card);
                }
            }
            
        });
    }


    protected void displayOpenRound(RoundProxy response) {
        getView().getRoundTitle().setText(response.getTitle());
    }


    @Override
    protected void revealInParent() {
        
    }

}
